/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package system;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Dhruv
 */

//table model class for customisation
public class MyTableModel extends AbstractTableModel {

    //column header names
    private String[] columnNames = {"time received", "sender","title","cc"};
    //holds information for table
    ArrayList<Object> data = new ArrayList<Object>();
    //holds information for each email
    ArrayList<EmailMetadata> tableMeta = new ArrayList<EmailMetadata>();
    //folder name
    private final String folder;

    //constructor
    public MyTableModel() {
        folder = "";
    }

    //constructor
    public MyTableModel(String folder, ArrayList<EmailMetadata> tableD) {
        this.folder = folder;
        setTableData(tableD);
    }

    //folder name identifies the object
    public String toString() {
        return folder;
    }

    //sets all the data into the model
    public void setTableData(ArrayList<EmailMetadata> tableD){
        setTableMetaData(tableD);
        data.removeAll(data);
        for(int i=0;i<tableD.size();i++){
            EmailMetadata temp = tableD.get(i);
            data.add(temp.getDatetime());
            data.add(temp.getSender());
            data.add(temp.getTitle());
            data.add(temp.getCC());
        }
    }

    //all the e-mail metadata of the table is set
    public void setTableMetaData(ArrayList<EmailMetadata> tableD){
        tableMeta = tableD;
    }

    //all the e-mail metadata of the table is returned
    public ArrayList<EmailMetadata> getTableMetaData(){
        return tableMeta;
    }

    //how many colums are there
    public int getColumnCount() {
        return columnNames.length;
    }

    //how many rows are there
    public int getRowCount() {
        return data.size()/this.getColumnCount();
    }

    //get a particular column name
    public String getColumnName(int col) {
        return columnNames[col];
    }

    //get the value of a particular cell
    public Object getValueAt(int row, int col) {
        return data.get(row*(this.getColumnCount())+col);
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    //table is not editable
    public boolean isCellEditable() {
        return false;
    }
}